#!/bin/bash
echo -e "\n\033[45;37m 开始联网 \033[1m" 

#安装usb网卡驱动 
[ $USER == pi ] && sudo insmod ~/系统配置/服务/8821cu_pi.ko
[ $USER == ubuntu ] && sudo insmod ~/系统配置/服务/8821cu_ubuntu.ko 
sleep 1


#============ 新建局域网 lan =========
echo -e "\n\n1 配置局域网 192.168.9.1/24 =====================\n"
sudo ip addr flush dev eth0

sudo brctl addbr lan     #  sudo ifconfig lan allmulti 混杂模式下时，网卡将来自接口的所有数据都捕获
sudo ifconfig lan 192.168.9.1 netmask 255.255.255.0 broadcast 192.168.9.255 hw ether 00:11:22:33:44:55 up

# DNS网关读取------>resolv-file=/etc/resolv.conf
# dhclient 获取的DNS网关保存在 /etc/resolv.conf
path=/run/dhcp.leases ;  sudo touch $path ; sudo chmod 777 $path 
path=/run/dnsmasq.conf ;  sudo touch $path ; sudo chmod 777 $path 

sudo cat > $path <<'END'
#如果仅为本机使用则写上127.0.0.1 ,如果用此计算机作为一组主机的默认 DNS，就需要使用固定 IP 地址
listen-address=127.0.0.1
interface=lan
#绑定了网卡之后会保证dnsmasq不去骚扰其他网卡，保证请求不乱发，一般跟interface一起使用
bind-interfaces
dhcp-range=192.168.9.1,192.168.9.254,255.255.255.0,12h
dhcp-option=option:router,192.168.9.1
resolv-file=/etc/resolv.conf
dhcp-leasefile=/run/dhcp.leases
no-hosts
address=/pi.me/192.168.9.1
address=/luyou.me/192.168.6.1
# 静态ip
#dhcp-host=01:4c:cc:6a:bb:83:67,192.168.9.100,infinite
END

sudo killall dnsmasq ; sleep 1
sudo dnsmasq -C /run/dnsmasq.conf --test
[ $? -eq 0 ] && sudo dnsmasq -C /run/dnsmasq.conf

sudo killall dhcpcd
sudo ip link set eth0 up  ; sudo brctl addif lan eth0

#===========

#===============2 nat转发 router nat==========================
echo -e "\n2 启动wl0端口 iptables NAT转发===============\n"

sudo sed -i 's@#net.ipv4.ip_forward=1@net.ipv4.ip_forward=1@g' /etc/sysctl.conf
sudo sysctl -p
sudo iptables -w -t nat -F   # sudo iptables -t nat -nvL
sudo iptables -w -t nat -I POSTROUTING  -s 192.168.9.0/24 -o wlan1 -j MASQUERADE

#sudo echo 1 > /proc/sys/net/ipv4/ip_forward
# > 是第二条命令，并没有sudo权限  用  tee
# echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
#=============

#===========3 检测usb网卡是否识别==================
echo -e "\n3 安装USB网卡rtl882lcu驱动====================\n"

path=/run/rtl8821cu_usbswitch ; sudo touch $path ; sudo chmod 777 $path 
sudo cat > $path <<'END'
# rtl8821cu
DefaultVendor=0x0bda
DefaultProduct=0x1a2b
TargetVendor=0x0bda
TargetProduct=0xc820
StandardEject=1
END

sudo usb_modeswitch -c /run/rtl8821cu_usbswitch      #手动切换网卡模式

#===========================



#=================4 USB网卡联网 wifi_connect==================
echo -e "\n4 USB网卡rtl8821cu联网========================\n"

path=/run/net_wpa_supplicant.conf ; sudo touch $path;  sudo chmod 777 $path 
sudo cat > $path <<'END'
update_config=1
ctrl_interface=/var/run/net_wpa_supplicant
network={
   ssid="U_5G"
   scan_ssid=1
   psk="55555666666"
}
END
sudo killall wpa_supplicant ;sudo killall dhclient ; sleep 1
sudo ip link set wlan1 up
sudo wpa_supplicant -B -i wlan1 -Dnl80211 -c/run/net_wpa_supplicant.conf

# dhclient 获取的DNS网关保存在 /etc/resolv.conf
sudo dhclient wlan1  &>/dev/null  &    #放到后台，否则卡住终端

#===========================

#==============5 hostapd============================
echo -e "\n5 开启 树莓派内部网卡热点: berry ==================\n"

path=/run/ap_hostapd.conf ; sudo touch $path;  sudo chmod 777 $path 
sudo cat > $path <<'END'
interface=wlan0
driver=nl80211
bridge=lan
hw_mode=a
ieee80211n=1
ieee80211ac=1
ieee80211d=1
ieee80211h=1
require_ht=1
require_vht=1
wmm_enabled=1
country_code=US
vht_oper_chwidth=1
channel=157
vht_oper_centr_freq_seg0_idx=155
ht_capab=[HT40-][HT40+][SHORT-GI-40][DSSS_CCK-40]
ssid=berry
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=li999000
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
END
sudo killall hostapd ; sudo rfkill unblock 0       #开启wlan0电源， rfkill list 查询
sleep 1
sudo ip link set wlan0 up
sudo ip addr flush dev wlan0
sudo hostapd /run/ap_hostapd.conf -B &>/dev/null



echo -e "\033[45;37m 联网操作结束 \033[0m\n" 

exit 0
